/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aal.business;

import com.aal.bean.CategoryFacadeLocal;
import com.aal.bean.ProductFacadeLocal;
import com.aal.entity.Category;
import com.aal.entity.Product;
import java.util.Collection;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;

/**
 *
 * @author quy
 */
@Stateless
public class ProductManagement implements ProductManagementLocal {

    @EJB
    private CategoryFacadeLocal categoryFacade;
    @EJB
    private ProductFacadeLocal productFacade;

    @Override
    public boolean addProduct(int categoryId, String productName, double unitPrice, String description, double taxPercent, String productImage, int status,double shippingpricecost) {
        try {
            Product product = new Product();
            product.setProductID(Integer.MIN_VALUE);
            product.setCategoryID(categoryFacade.find(categoryId));
            product.setProductName(productName);
            product.setUnitPrice(unitPrice);
           
            product.setDescription(description);
            product.setTaxPercent(taxPercent);
            product.setProductImage(productImage);
            product.setStatus(status);
            product.setShippingPriceCost(shippingpricecost);
            productFacade.create(product);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    public boolean editProduct(int productid, int categoryId, String productName, double unitPrice, String description, double taxPercent, String productImage, int status,double shippingpricecost) {
        try {
            Product product = productFacade.findName(productName);
            product.setCategoryID(categoryFacade.find(categoryId));
            product.setUnitPrice(unitPrice);
            product.setDescription(description);
            product.setTaxPercent(taxPercent);
            product.setProductImage(productImage);
            product.setStatus(status);
            product.setShippingPriceCost(shippingpricecost);
            productFacade.edit(product);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    @Override
    public List<Product> searchproductbyName(String productName) {
        return productFacade.findProduct(productName);
    }

    @Override
    public Product searchproductbyID(int ID) {
        return productFacade.find(ID);
    }

    @Override
    public List<Product> listallproducts() {
        return productFacade.findAll();
    }

    @Override
    public Collection<Product> searchproductbyCategory(int categoryId) {
        Category category = categoryFacade.find(categoryId);
        return category.getProductCollection();
    }

    @Override
    public boolean editProduct(String productName, int categoryId, double unitPrice, String description, double taxPercent, String productImage, int status, double shippingpricecost) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
